#include "controllable_manager.h"

ControllableManager* ControllableManager::thisPointer;

ControllableManager::ControllableManager(void)
{
  this->pHead = NULL;
  ControllableManager::thisPointer = this;
}


ControllableManager::~ControllableManager(void)
{
}


void ControllableManager::Controls(bool key_down, int key_code) {
  List *temp = pHead;
  while(temp != NULL) {
    temp->object->DoControl(key_down, key_code);
    temp = temp->pNext;
  }
}


void ControllableManager::Threading() {
    List *temp = pHead;
  while(temp != NULL) {
    temp->object->DoControlThread();
    temp = temp->pNext;
  }
}


void ControllableManager::Add(ControllableObject *object) {
  if(this->pHead == NULL) {
    pHead = new List;
    this->pHead->object = object;
    this->pHead->pNext = NULL;
  } else {
    List *temp = new List;
    memcpy(temp, pHead, sizeof(List));
    pHead->object = object;
    pHead->pNext = temp;
  }
}


void ControllableManager::Delete(ControllableObject *object) {
  if(pHead == NULL)
    return;
  if(pHead->object == object) {
    if(pHead->pNext == NULL) {
      pHead = NULL;
      return;
    } else {
      pHead = pHead->pNext;
      return;
    }
  }
  List *pre = pHead;
  List *temp = pHead->pNext;
  while(temp != NULL) {
    if(temp->object == object) {
      pre->pNext = temp->pNext;
      pre = NULL;
      temp = NULL;
      return;
    } else {
      pre = temp;
      temp = temp->pNext;
    }
  }
}


void ControllableManager::RemoveAll() {

}

